import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * model 消息模板
 * @date: 2018-7-26


 */
import { createPagination, getResponse, parseParameters } from 'utils/utils';
import { search, initPara as _initPara, editPara as _editPara, deletePara as _deletePara, deleteItem as _deleteItem, searchDetail, addTemplate as _addTemplate, updateTemplate as _updateTemplate, fetchDetailPara as _fetchDetailPara, searchCategoryCodeTree, copyDetail } from "../services/messageTemplateService"; // import { queryIdpValue } from '../../services/api';

/**
 * 定义消息模板功能数据源及处理方法
 */

export default {
  namespace: 'messageTemplate',
  // model名称
  state: {
    list: [],
    // 数据展示列表
    pagination: {},
    // 分页器
    detail: {},
    // 消息模板明细
    categoryTree: [],
    // 消息子类型
    language: [],
    // 语言数据列表
    paraList: [],
    // 模板参数列表
    paraPagination: {},
    // 模板参数列表分页器
    copyDetail: {} // 复制详情

  },
  effects: {
    // 获取消息子类型
    fetchType: /*#__PURE__*/_regeneratorRuntime.mark(function fetchType(_, _ref) {
      var call, put, categoryTree;
      return _regeneratorRuntime.wrap(function fetchType$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.t0 = getResponse;
              _context.next = 4;
              return call(searchCategoryCodeTree, {
                'CMSG.MESSAGE_CATEGORY': 1,
                'CMSG.MESSAGE_SUBCATEGORY': 2
              });

            case 4:
              _context.t1 = _context.sent;
              categoryTree = (0, _context.t0)(_context.t1);
              _context.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  categoryTree: categoryTree
                }
              });

            case 8:
            case "end":
              return _context.stop();
          }
        }
      }, fetchType);
    }),
    // 消息模板列表数据获取
    fetchTemplate: /*#__PURE__*/_regeneratorRuntime.mark(function fetchTemplate(_ref2, _ref3) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function fetchTemplate$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              call = _ref3.call, put = _ref3.put;
              _context2.next = 4;
              return call(search, payload);

            case 4:
              result = _context2.sent;
              result = getResponse(result);

              if (!result) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  list: result.content,
                  pagination: createPagination(result),
                  detail: {},
                  copyDetail: {}
                }
              });

            case 9:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchTemplate);
    }),
    fetchDetail: /*#__PURE__*/_regeneratorRuntime.mark(function fetchDetail(_ref4, _ref5) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function fetchDetail$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              call = _ref5.call, put = _ref5.put;
              _context3.next = 4;
              return call(searchDetail, payload);

            case 4:
              result = _context3.sent;
              result = getResponse(result);
              _context3.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  detail: result
                }
              });

            case 8:
              return _context3.abrupt("return", result);

            case 9:
            case "end":
              return _context3.stop();
          }
        }
      }, fetchDetail);
    }),
    // 删除消息模板
    deleteItem: /*#__PURE__*/_regeneratorRuntime.mark(function deleteItem(_ref6, _ref7) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function deleteItem$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call;
              _context4.next = 4;
              return call(_deleteItem, payload);

            case 4:
              result = _context4.sent;
              return _context4.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, deleteItem);
    }),
    fetchCopyDetail: /*#__PURE__*/_regeneratorRuntime.mark(function fetchCopyDetail(_ref8, _ref9) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function fetchCopyDetail$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              call = _ref9.call, put = _ref9.put;
              _context5.next = 4;
              return call(copyDetail, payload);

            case 4:
              result = _context5.sent;
              result = getResponse(result);
              _context5.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  copyDetail: result
                }
              });

            case 8:
              return _context5.abrupt("return", result);

            case 9:
            case "end":
              return _context5.stop();
          }
        }
      }, fetchCopyDetail);
    }),
    fetchDetailPara: /*#__PURE__*/_regeneratorRuntime.mark(function fetchDetailPara(_ref10, _ref11) {
      var payload, call, put, res, data;
      return _regeneratorRuntime.wrap(function fetchDetailPara$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref10.payload;
              call = _ref11.call, put = _ref11.put;
              _context6.next = 4;
              return call(_fetchDetailPara, parseParameters(payload));

            case 4:
              res = _context6.sent;
              data = getResponse(res);
              _context6.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  paraList: data && data.content,
                  paraPagination: createPagination(data)
                }
              });

            case 8:
              return _context6.abrupt("return", data);

            case 9:
            case "end":
              return _context6.stop();
          }
        }
      }, fetchDetailPara);
    }),
    // 初始化模板参数
    initPara: /*#__PURE__*/_regeneratorRuntime.mark(function initPara(_ref12, _ref13) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function initPara$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref12.payload;
              call = _ref13.call;
              _context7.next = 4;
              return call(_initPara, payload);

            case 4:
              result = _context7.sent;
              return _context7.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context7.stop();
          }
        }
      }, initPara);
    }),
    // 删除模板参数
    deletePara: /*#__PURE__*/_regeneratorRuntime.mark(function deletePara(_ref14, _ref15) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function deletePara$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref14.payload;
              call = _ref15.call;
              _context8.next = 4;
              return call(_deletePara, payload);

            case 4:
              result = _context8.sent;
              return _context8.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context8.stop();
          }
        }
      }, deletePara);
    }),
    // 编辑模板参数
    editPara: /*#__PURE__*/_regeneratorRuntime.mark(function editPara(_ref16, _ref17) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function editPara$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              payload = _ref16.payload;
              call = _ref17.call;
              _context9.next = 4;
              return call(_editPara, payload);

            case 4:
              result = _context9.sent;
              return _context9.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context9.stop();
          }
        }
      }, editPara);
    }),
    // 更新模板
    updateTemplate: /*#__PURE__*/_regeneratorRuntime.mark(function updateTemplate(_ref18, _ref19) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function updateTemplate$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              payload = _ref18.payload;
              call = _ref19.call;
              _context10.next = 4;
              return call(_updateTemplate, payload);

            case 4:
              result = _context10.sent;
              return _context10.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context10.stop();
          }
        }
      }, updateTemplate);
    }),
    // 添加模板
    addTemplate: /*#__PURE__*/_regeneratorRuntime.mark(function addTemplate(_ref20, _ref21) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function addTemplate$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              payload = _ref20.payload;
              call = _ref21.call;
              _context11.next = 4;
              return call(_addTemplate, payload);

            case 4:
              result = _context11.sent;
              return _context11.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context11.stop();
          }
        }
      }, addTemplate);
    })
  },
  reducers: {
    // 合并state状态数据,生成新的state
    updateState: function updateState(state, _ref22) {
      var payload = _ref22.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};